import 'package:flutter/material.dart';
import 'package:terminal_flutter_demo/routers/routers.dart';

// ignore: must_be_immutable
class NavigatorPage extends StatefulWidget {
  NavigatorPage({Key? key, this.title = '主页', this.count = 0, this.arguments})
      : super(key: key);

  String title;

  int count;

  Map? arguments;

  @override
  _NavigatorPageState createState() => _NavigatorPageState();
}

class _NavigatorPageState extends State<NavigatorPage> {
  @override
  Widget build(BuildContext context) {
    print('_NavigatorPageState arguments: ${widget.arguments}');
    return Scaffold(
      appBar: AppBar(
        title: Text(
            '${widget.title}-${widget.count != 0 ? widget.count : widget.arguments?['count']}'),
      ),
      body: Center(
        child: Column(
          children: [
            const SizedBox(
              height: 20,
            ),
            Navigator.of(context).canPop()
                ? ElevatedButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    child: const Text('跳转上一页'),
                  )
                : const Text(''),
            const SizedBox(
              height: 20,
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).push(MaterialPageRoute(
                  builder: ((BuildContext context) {
                    return NavigatorPage(
                      title: '路由跳转',
                      count: ++widget.count,
                    );
                  }),
                  settings: RouteSettings(name: widget.title),
                ));
                // Navigator.of(context).pushReplacement(newRoute)
              },
              child: const Text('跳转下一页'),
            ),
            SizedBox(
              height: 20,
              child: Text('${ModalRoute.of(context)?.settings.name}'),
            ),
            ModalRoute.of(context)?.settings.name == '/'
                ? ElevatedButton(
                    onPressed: () {
                      Navigator.of(context).push(MaterialPageRoute(
                        builder: ((BuildContext context) {
                          return NavigatorRouterPage(
                            title: '跳转到命名路由首页',
                            arguments: const {
                              'title': '命名路由首页',
                              'count': 0,
                            },
                          );
                        }),
                        settings: const RouteSettings(
                          name: '/',
                          arguments: {
                            'title': '命名路由首页',
                            'count': 1,
                          },
                        ),
                      ));
                    },
                    child: const Text('跳转到命名路由首页'),
                  )
                : ElevatedButton(
                    onPressed: () {
                      if (ModalRoute.of(context)?.settings.name == '/shop') {
                        Navigator.of(context).pop();
                      } else {
                        Navigator.of(context).pushNamed('/shop', arguments: {
                          'title': '命名路由跳转下一页',
                          'count': ++widget.arguments?['count'],
                        });
                        // Navigator.of(context).pushReplacementNamed('routeName');
                        // Navigator.of(context).pushAndRemoveUntil(
                        //   MaterialPageRoute(
                        //     builder: ((BuildContext context) {
                        //       return NavigatorPage(
                        //         title: '路由跳转',
                        //         count: ++widget.count,
                        //       );
                        //     }),
                        //     settings: RouteSettings(name: widget.title),
                        //   ),
                        //   (route) => false,
                        // );
                      }
                    },
                    child: Text(ModalRoute.of(context)?.settings.name == '/shop'
                        ? '命名路由跳转上一页'
                        : '命名路由跳转下一页'),
                  ),
          ],
        ),
      ),
    );
  }
}

// ignore: must_be_immutable
class NavigatorRouterPage extends StatefulWidget {
  NavigatorRouterPage({Key? key, required this.title, this.arguments})
      : super(key: key);

  final String title;

  Map? arguments;

  @override
  _NavigatorRouterPageState createState() => _NavigatorRouterPageState();
}

class _NavigatorRouterPageState extends State<NavigatorRouterPage> {
  final MyRouteObserver<PageRoute> routeObserver = MyRouteObserver<PageRoute>();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // home: Scaffold(
      //   appBar: AppBar(
      //     title: Text(
      //         '${widget.arguments['title']}: ${widget.arguments['count']}'),
      //   ),
      //   // body: Column(
      //   //   children: [
      //   //     const SizedBox(
      //   //       height: 20,
      //   //     ),
      //   //     Navigator.of(context).canPop()
      //   //         ? ElevatedButton(
      //   //             onPressed: () {
      //   //               Navigator.of(context).pop();
      //   //             },
      //   //             child: const Text('命名路由跳转跳转上一页'),
      //   //           )
      //   //         : const Text(''),
      //   //     const SizedBox(
      //   //       height: 20,
      //   //     ),
      //   //     ElevatedButton(
      //   //       onPressed: () {
      //   //         print('跳转下一页');
      //   //         Navigator.of(context).pushNamed(
      //   //           '/news',
      //   //           arguments: {
      //   //             'title': 'title shop',
      //   //             'count': ++widget.arguments['count'],
      //   //           },
      //   //         );
      //   //       },
      //   //       child: const Text('命名路由跳转下一页shop'),
      //   //     ),
      //   //   ],
      //   // ),
      // ),
      initialRoute: '/news',
      // routes: {
      //   '/': (context) => NavigatorPage(title: 'title'),
      // },
      onGenerateRoute: onGenerateRoute,
      navigatorObservers: [
        routeObserver,
      ],
    );
  }
}
